package leetcode.code0430;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;
import leetcode.helper.tree.Node;

public abstract class Solution430 implements IDebug {

	public abstract Node flatten(Node head);

	@Override
	public void debug1() {
		String str1 = "[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]";
		String str2 = "[1,2,3,7,8,11,12,9,10,4,5,6]";
		Node head = this.setChildNode(str1);
		Node r = this.flatten(head);
		Node e = Node.array2tree1(HelpDebug.str2array4(str2));
		HelpDebug.compare(r, e);
	}

	/**
	 * [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12] <br>
	 * []
	 *
	 * @param str
	 * @return
	 */
	public Node setChildNode_不简洁(String str) {
		if (str == null)
			return null;
		str = str.replace("[", "").replace("]", "").trim();
		if (str.equals(""))
			return null;
		String[] array = str.split(",");
		Node head = new Node();
		head.val = Integer.valueOf(array[0]);
		int p = 1;
		Node cur = head;
		Node childH = head;
		Node childT = head;
		int cNull = 0;
		while (p < array.length) {
			String pStr = array[p];
			if (!pStr.equals("null")) {
				if (cNull == 0) {
					Node node = new Node();
					node.val = Integer.valueOf(pStr);
					cur.next = node;
					cur = cur.next;
					cNull = 0;
					p++;
					childT = node;
				} else {
					while (cNull > 1) {
						childH = childH.next;
						cNull--;
					}
					Node node = new Node();
					node.val = Integer.valueOf(pStr);
					cur.next = node;
					cur = cur.next;
					cNull = 0;
					p++;
					childT.next = null;
					childH.child = node;
					childH = node;
					childT = node;
				}
			} else {
				cNull++;
				p++;
			}
		}
		return head;
	}

	public Node setChildNode(String str) {
		if (str == null)
			return null;
		str = str.replace("[", "").replace("]", "").trim();
		if (str.equals(""))
			return null;
		String[] array = str.split(",");
		Node head = new Node();
		head.val = Integer.valueOf(array[0]);
		int p = 1;
		Node cur = head;
		Node childH = head;
		Node childT = head;
		int cNull = 0;
		while (p < array.length) {
			String pStr = array[p];
			if (!pStr.equals("null")) {
				if (cNull == 0) {
					Node node = new Node();
					node.val = Integer.valueOf(pStr);
					cur.next = node;
					cur = cur.next;
					cNull = 0;
					p++;
					childT = node;
				} else {
					while (cNull > 1) {
						childH = childH.next;
						cNull--;
					}
					Node node = new Node();
					node.val = Integer.valueOf(pStr);
					cur.next = node;
					cur = cur.next;
					cNull = 0;
					p++;
					childT.next = null;
					childH.child = node;
					childH = node;
					childT = node;
				}
			} else {
				cNull++;
				p++;
			}
		}
		return head;
	}

	@Override
	public void debug2() {
		String str1 = "[1,2,null,3]";
		String str2 = "[1,3,2]";
		Node head = this.setChildNode(str1);
		Node r = this.flatten(head);
		Node e = Node.array2tree1(HelpDebug.str2array4(str2));
		HelpDebug.compare(r, e);
	}

	@Override
	public void debug3() {
		String str1 = "[]";
		String str2 = "[]";
		Node head = this.setChildNode(str1);
		Node r = this.flatten(head);
		Node e = Node.array2tree1(HelpDebug.str2array4(str2));
		HelpDebug.compare(r, e);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

}
